package com.tour.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tour.config.ResponseWrapper;
import com.tour.entity.Discuss;
import com.tour.entity.Post;
import com.tour.mapper.DiscussMapper;
import com.tour.mapper.PostMapper;
import com.tour.service.PostService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lel
 * @since 2020-12-30
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post> implements PostService {

    @Autowired
    PostMapper postMapper;

    @Autowired
    DiscussMapper discussMapper;

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    public ResponseWrapper addPost(Post post){
        if (post != null){
            int result = postMapper.insert(post);
            if (result == 1){
                return ResponseWrapper.markCustom(true,200,"添加成功",null);
            }else {
                return ResponseWrapper.markError();
            }
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper findPostByCategory(String category,Integer pageSize,Integer pageNum){
        Page<Map> page = new Page<>(pageNum,pageSize);
        IPage<Map> postList = postMapper.findPostByCategory(page,category);
        List list =postList.getRecords();
        if (list.size()>0){
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(postList);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",postList.getTotal());
            wrapper.getData().put("pageNum",postList.getCurrent());
            wrapper.getData().put("pageSize",postList.getSize());
            return wrapper;
        }else {
            return ResponseWrapper.markError();
        }
    }
    public ResponseWrapper delPost(Integer postId){
        if (postId != null){
            int i = postMapper.deleteById(postId);
            if (i ==1){
                return ResponseWrapper.markCustom(true,200,"删除一条游记",null);
            }else {
                return ResponseWrapper.markError();
            }
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper findPostById(Integer postId,Integer pageSize,Integer pageNum){
        if (postId == null){
            return ResponseWrapper.markError();
        }else {
            Map post = postMapper.findPostById(postId);
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> iPage = discussMapper.discussList(page,postId);
            List discussList= iPage.getRecords();
            ResponseWrapper  wrapper = ResponseWrapper.markSuccess(post);
            wrapper.getData().put("data",post);
            wrapper.getData().put("discuss",discussList);
            wrapper.getData().put("total",iPage.getTotal());
            wrapper.getData().put("pageSize",iPage.getSize());
            wrapper.getData().put("pageNum",iPage.getCurrent());
            wrapper.getData().put("discussCount",discussList.size());
            return wrapper;
        }
    }
    public ResponseWrapper findPostByuserId(Integer userId,Integer pageSize,Integer pageNum){
        if (userId == null){
            return ResponseWrapper.markError();
        }else {
            Page<Map> page = new Page<>(pageNum,pageSize);
            IPage<Map> postList = postMapper.findPostByuserId(page,userId);
            List list =postList.getRecords();
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",postList.getTotal());
            wrapper.getData().put("pageNum",postList.getCurrent());
            wrapper.getData().put("pageSize",postList.getSize());
            return wrapper;
        }
    }

    public ResponseWrapper updateLikes(Integer postId,Integer likes){
        Post post = postMapper.selectById(postId);
        post.setLikes(likes);
        int result = postMapper.updateById(post);
        if (result == 1){
            return ResponseWrapper.markCustom(true,200,"修改一条数据",null);
        }else {
            return ResponseWrapper.markError();
        }
    }

    public ResponseWrapper seachPost(String content,Integer pageSize,Integer pageNum){
        Page<Map> page = new Page<>(pageNum,pageSize);
        IPage<Map> postList = postMapper.seachPost(page,content);
        List list =postList.getRecords();
        if (list.size()>0){
            ResponseWrapper wrapper = ResponseWrapper.markSuccess(list);
            wrapper.getData().put("data",list);
            wrapper.getData().put("total",postList.getTotal());
            wrapper.getData().put("pageNum",postList.getCurrent());
            wrapper.getData().put("pageSize",postList.getSize());
            return wrapper;
        }else {
            return ResponseWrapper.markError();
        }
    }
}
